﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SocialFilter.model;

namespace SocialFilter
{

    //This class is programmed such that each instance is bound 
    //to a particular token (and consequently, a particular user)
    class FacebookQuerier : SocialFilter.controller.DataSourceQuerier
    {


        /*public static void Main()
        {
            //FacebookTest test = new FacebookTest("169651206390011%7Ccea87d51c85df2ae38f48102-12819133%7CWPtTvAaP0K2_mAfaXLOoPH0ggAQ");
            FacebookQuerier test = new FacebookQuerier("169651206390011|cea87d51c85df2ae38f48102-12819133|WPtTvAaP0K2_mAfaXLOoPH0ggAQ");

            test.getBasicInfo();
            test.getFriendIDs();
            test.RetrieveItems("whatever", "169651206390011|cea87d51c85df2ae38f48102-12819133|WPtTvAaP0K2_mAfaXLOoPH0ggAQ");
        }*/

        private Facebook.FacebookAPI api;
        private int uid; 

        // Constructor
        public FacebookQuerier(String token)
        {
            this.api = new Facebook.FacebookAPI(token); 
        }

        public FacebookQuerier()
        {
            this.api = null; 
        }

        //Returns a dictionary of the user's basic profile information
        /*public Dictionary<string, string> getBasicInfo()
        {

            //Gets basic profile information
            Facebook.JSONObject basicProfile = this.api.Get("/charlie.shilling");

            Dictionary<string, string> basicInfo = new Dictionary<string, string>()
            {
                {"id", basicProfile.Dictionary["id"].String},
                {"name", basicProfile.Dictionary["name"].String},
                {"first_name", basicProfile.Dictionary["first_name"].String},
                {"last_name", basicProfile.Dictionary["last_name"].String},
                {"link", basicProfile.Dictionary["link"].String},
                {"about", basicProfile.Dictionary["about"].String},
                {"birthday", basicProfile.Dictionary["birthday"].String},
                {"hometown", basicProfile.Dictionary["hometown"].String},
                {"location", basicProfile.Dictionary["location"].String},
                {"bio", basicProfile.Dictionary["bio"].String},
                {"quotes", basicProfile.Dictionary["quotes"].String},
                {"work", basicProfile.Dictionary["work"].String},
                {"education", basicProfile.Dictionary["education"].String},
                {"gender", basicProfile.Dictionary["gender"].String},
                {"interested_in", basicProfile.Dictionary["interested_in"].String},
                {"realtionship_status", basicProfile.Dictionary["relationship_status"].String},
                {"religion", basicProfile.Dictionary["religion"].String},
                {"political", basicProfile.Dictionary["political"].String},
                {"email", basicProfile.Dictionary["email"].String},
                {"timezone", basicProfile.Dictionary["timezone"].String},
                {"locale", basicProfile.Dictionary["locale"].String},
                {"verified", basicProfile.Dictionary["verified"].String},
                {"updated_time", basicProfile.Dictionary["updated_time"].String}
            };

            return basicInfo; 
        }*/
        

        //public Dictionary<String, Object>[] getNewsFeed()
        public override List<FeedItem> RetrieveItems(string _username, string _token)
        {

            this.api = new Facebook.FacebookAPI(_token);

            String content, author, link, timeString;
            DateTime createTime; 
            int feedSize;
            List<FeedItem> feedList;
            const String source = "facebook";

            //Gets news feed
            Facebook.JSONObject feed = this.api.Get("/charlie.shilling/home");

            feedSize = feed.Dictionary["data"].Array.Length;
            feedList = new List<FeedItem>(feedSize);

            for (int i = 0; i < feedSize; i++)
            {
                String type = feed.Dictionary["data"].Array[i].Dictionary["type"].String;
                
                try
                {
                    if (String.Compare(type, "link", true) == 0) 
                        content = feed.Dictionary["data"].Array[i].Dictionary["description"].String;
                    else if (String.Compare(type, "video", true) == 0)
                        content = feed.Dictionary["data"].Array[i].Dictionary["name"].String;
                    else
                        content = feed.Dictionary["data"].Array[i].Dictionary["message"].String;
                }
                catch (Exception e)
                {
                    content = null;
                }

                try
                {
                    author = feed.Dictionary["data"].Array[i].Dictionary["from"].Dictionary["name"].String;
                }
                catch (Exception e)
                {
                    author = null;
                }

                try
                {
                    timeString = feed.Dictionary["data"].Array[i].Dictionary["type"].String;
                    int year = Int32.Parse(timeString.Substring(0, 4));
                    int month = Int32.Parse(timeString.Substring(5, 2));
                    int day = Int32.Parse(timeString.Substring(8, 2));
                    int hour = Int32.Parse(timeString.Substring(11, 2));
                    int minute = Int32.Parse(timeString.Substring(14, 2));
                    int second = Int32.Parse(timeString.Substring(17, 2));
                    createTime = new DateTime(year, month, day, hour, minute, second); 
                }
                catch (Exception e)
                {
                    createTime = new DateTime();
                }

                try
                {
                    link = feed.Dictionary["data"].Array[i].Dictionary["link"].String;
                }
                catch (Exception e)
                {
                    link = null;
                }

                FeedItem newItem = new FeedItem(content, source, author, createTime, link);
                feedList.Add(newItem);

            }

            return feedList;
        }
    }
}
